<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 菜单显示组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */

class Widget_Menu extends Widget
{
    /**
     * 当前父菜单
     *
     * @access public
     * @var integer
     */
    public $currentParent;

    /**
     * 当前子菜单
     *
     * @access public
     * @var integer
     */
    public $currentChild;
	
    /**
     * 当前页面
     *
     * @access public
     * @var string
     */
    public $currentUri;

    /**
     * 当前菜单标题
     * @var string
     */
    public $currentTitle;
    
    /**
     * 当前项目链接
     * @var string
     */
    public $currentUrl;
	
    /**
     * 当前增加项目链接
     * @var string
     */
    public $currentAddLink;

    /**
     * 执行函数,初始化菜单
     *
     * @access public
     * @return void
     */
    public function execute()
    {
		$parentNodes =array(_t('控制台'), _t('设置'), _t('管理'));
        $childNodes = array(
		    array(
                array(_t('首页'), _t('后台首页'), $this->options->ucenterUrl('index', false), 'member'),
                array(_t('插件'), _t('插件管理'), $this->options->ucenterUrl('plugins', false), 'admin'),
                array(array('Widget_Plugins_Config', 'getMenuTitle'), array('Widget_Plugins_Config', 'getMenuTitle'), $this->options->ucenterUrl('options-plugin?config=', false), 'admin', true),
                array(_t('外观'), _t('网站外观'), $this->options->ucenterUrl('themes', false), 'admin'),
                array(array('Widget_Themes_Files', 'getMenuTitle'), array('Widget_Themes_Files', 'getMenuTitle'), $this->options->ucenterUrl('theme', false), 'admin', true),
                array(_t('设置外观'), _t('设置外观'), $this->options->ucenterUrl('options-theme', false), 'admin', true)		
		    ),
		    array(	
                array(_t('基本'), _t('基本设置'), $this->options->ucenterUrl('options-general', false), 'admin'),
                array(_t('评论'), _t('评论设置'), $this->options->ucenterUrl('options-discussion', false), 'admin'),
                array(_t('阅读'), _t('阅读设置'), $this->options->ucenterUrl('options-reading', false), 'admin'),
                array(_t('永久链接'), _t('永久链接设置'), $this->options->ucenterUrl('options-permalink', false), 'admin'),
                array(_t('个人资料'), _t('个人资料设置'), $this->options->ucenterUrl('user-profile', false), 'member'),
                array(_t('密码'), _t('密码修改'), $this->options->ucenterUrl('user-password', false), 'member')	
		    ),
		    array(
                array(_t('文章'), _t('管理文章'), $this->options->ucenterUrl('posts', false), 'contributor', false, $this->options->ucenterUrl('post', false)),
                array(array('Widget_Contents_Post_Edit', 'getMenuTitle'), array('Widget_Contents_Post_Edit', 'getMenuTitle'), $this->options->ucenterUrl('post', false), 'contributor', true),
                array(_t('独立页面'), _t('管理独立页面'), $this->options->ucenterUrl('pages', false), 'editor', false, $this->options->ucenterUrl('page', false)),
                array(array('Widget_Contents_Page_Edit', 'getMenuTitle'), array('Widget_Contents_Page_Edit', 'getMenuTitle'), $this->options->ucenterUrl('page', false), 'editor', true),
                array(_t('评论'), _t('管理评论'), $this->options->ucenterUrl('comments', false), 'contributor'),
                array(_t('分类'), _t('管理分类'), $this->options->ucenterUrl('categories', false), 'editor', false, $this->options->ucenterUrl('category', false)),
                array(array('Widget_Metas_Category_Edit', 'getMenuTitle'), array('Widget_Metas_Category_Edit', 'getMenuTitle'), $this->options->ucenterUrl('category', false), 'editor', true),
                array(_t('标签'), _t('管理标签'), $this->options->ucenterUrl('tags', false), 'editor', false, $this->options->ucenterUrl('tag', false)),
                array(array('Widget_Metas_Tag_Edit', 'getMenuTitle'), array('Widget_Metas_Tag_Edit', 'getMenuTitle'), $this->options->ucenterUrl('tag', false), 'editor', true),				
                array(_t('用户'), _t('管理用户'), $this->options->ucenterUrl('users', false), 'admin', false, $this->options->ucenterUrl('user', false)),
                array(array('Widget_Users_Edit', 'getMenuTitle'), array('Widget_Users_Edit', 'getMenuTitle'), $this->options->ucenterUrl('user', false), 'admin', true)
		    ),
	    );
		$panelTable = 'panelTable';
		$baseUrl = rtrim($this->options->ucenterUrl, '/');
		
        /** 获取扩展菜单 */
        $panelTable = unserialize($this->options->{$panelTable});
        $extendingParentMenu = empty($panelTable['parent']) ? array() : $panelTable['parent'];
        $extendingChildMenu = empty($panelTable['child']) ? array() : $panelTable['child'];
        $currentUrl = rtrim($this->request->makeUriByRequest(), '/');
        $menu = array();
		$defaultChildeNode = array(NULL, NULL, NULL, 'admin', false, NULL);

        $currentUrlParts = parse_url($currentUrl);
        $currentUrlParams = array();
        if (!empty($currentUrlParts['query'])) {
            parse_str($currentUrlParts['query'], $currentUrlParams);
        }

        if ($baseUrl == $currentUrl) {
            $currentUrlParts['path'] .= '/index';
        }

        foreach ($extendingParentMenu as $key => $val) 
		{
            $parentNodes[10 + $key] = $val;
        }

        foreach ($extendingChildMenu as $key => $val) 
		{
            $childNodes[$key] = array_merge(isset($childNodes[$key]) ? $childNodes[$key] : array(), $val);
        }

        foreach ($parentNodes as $key => $parentNode) 
		{
            if (!$parentNode) {
                continue;
            }

            $children = array();
            $showedChildrenCount = 0;
			$firstUrl = NULL;
			
			$menu[$key]['name'] = $parentNode;

            foreach ($childNodes[$key] as $inKey => $childNode) 
			{
				$childNode += $defaultChildeNode;
				list ($name, $title, $url, $access, $hidden, $addLink) = $childNode;
				
                // 保存最原始的hidden信息
                $orgHidden = $hidden;
				
                if (NULL !== $access && !$this->user->pass($access, true)) {
                    continue;
                }

                // compare url
                $urlParts = parse_url($url);
                $urlParams = array();
                if (!empty($urlParts['query'])) {
                    parse_str($urlParts['query'], $urlParams);
                }

                $validate = true;
                if ($urlParts['path'] != $currentUrlParts['path']) {
                    $validate = false;
                } else {
                    foreach ($urlParams as $paramName => $paramValue) 
					{
                        if (!isset($currentUrlParams[$paramName])) {
                            $validate = false;
                            break;
                        }
                    }
                }
                
                if ($hidden && $validate) {
                    $hidden = false;
                }

                if (!$hidden) {

                    $showedChildrenCount ++;

                    if (NULL == $firstUrl) {
						$firstUrl = $url;
                    }

                    if (is_array($name)) {
                        list($widget, $method) = $name;
                        $name = $this->widget($widget)->$method($this);
                    }
                    
                    if (is_array($title)) {
                        list($widget, $method) = $title;
                        $title = $this->widget($widget)->$method($this);
                    }
					
                    if (is_array($addLink)) {
                        list($widget, $method) = $addLink;
                        $addLink = $this->widget($widget)->$method($this);
                    }
                }

                if ($validate) {
                    $this->currentParent = $key;
                    $this->currentChild = $inKey;
                    $this->currentTitle = $title;
                    $this->currentUrl = $url;
					$this->currentAddLink = $addLink ? $addLink : NULL;
                }
				
                !$hidden && $children[$inKey] = array(
				        'current' => $validate,
                        'name' => $name,
                        'title' => $title,
                        'url' => $validate ? $currentUrl : $url,
					    'access' => $access,
					    'hidden' => $hidden,
					    'addLink' => $addLink,
					    'orgHidden' => $orgHidden,
						//'icon'=>base_convert(sprintf("%u", crc32($parentNode . '-' . $name)), 10, 36)
                );
            }

			if($showedChildrenCount) { 
			    $menu[$key] += array(
			        'current'=>$key === $this->currentParent, 
				    'count'=>$showedChildrenCount, 
				    'parent'=>1,
					'child'=>$children, 
				    'icon'=>base_convert(sprintf("%u", crc32($parentNode)), 10, 36)
			    );
			} else {
				$menu[$key] = null;
				unset($menu[$key]);
			}
        }

        $this->stack = $menu;
        $this->currentUri = $currentUrl;
    }	

}

